<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Word;
use App\Models\Category;
use Image;

class WordController extends Controller
{
    /**
     * 单词列表
     *
     * @param [type] $slug
     * @return void
     */
    public function index($slug = 'all')
    {
        if ($slug != 'all') {
            $category = Category::where('slug', $slug)->first();
//          $words=$category->words()->where('showtimes','<','2001')->orderBy('showtimes','desc')->paginate(16);
            $words = $category->words()->paginate(16);
        } else {
//          $words=Word::where('showtimes','<','2001')->orderBy('showtimes','desc')->paginate(16);
            $words = Word::paginate(16);
        }
        $cats = Category::all();
        //$words->withPath('word');
        return view('word.index', compact('words', 'slug', 'cats'));
    }

    /**
     * 分类展示单词外加开头结尾
     *
     * @param [type] $slug
     * @param [type] $word
     * @return void
     */
    public function show($slug = 'all', $word)
    {
        if ($slug != 'all') {
            $category = Category::where('slug', $slug)->first();
            $start = $category->wordstarts()->orderBy(\DB::raw('RAND()'))->first();
            $start = $start ? $start->switchword($word) : "";
            $end = $category->wordends()->orderBy(\DB::raw('RAND()'))->first();
            $end = $end ? $end->switchword($word) : "";
        } else {
            $start = "";
            $end = "";
        }

        $iword = Word::where('word', $word)->first();
        $iword->pron = json_decode($iword->pron);
        $iword->interpret = json_decode($iword->interpret);
        $iword->tips = str_replace('"', '', $iword->tips);
        $iword->interpretmore = json_decode($iword->interpretmore);
        $iword->sentences = json_decode($iword->sentences);
        $sens = $iword->sentences;
        shuffle($sens);
        //$words=$category->words->paginate(15);
        //$words->withPath('word');

        $cats = Category::all();
        //$iword=json_encode($iword,JSON_UNESCAPED_UNICODE, JSON_FORCE_OBJECT);
        //$iword=json_encode($item, JSON_UNESCAPED_UNICODE);
        return view('word.show', compact('iword', 'sens', 'start', 'end', 'cats'));
    }

    public function create_category()
    {
        $cats = Category::all();
        return view('word.category', compact('cats'));
    }

    public function store_category(Request $request)
    {
        $this->validate($request, [
            'category_id' => 'required',
            'words' => 'required'
        ]);
        $words = explode("\r\n", $request->words);
        $wordids = array();
        foreach ($words as $w) {
            $wid = Word::where('word', $w)->value('id');
            if ($wid) {
                array_push($wordids, $wid);
            }
        }
        $cat = Category::find($request->category_id);
        //存储关联的单词数据到word_category表
        $cat->words()->sync($wordids, false);

        return redirect()->route('word.category.create');
    }

}
